package com.chatsimple.netty

import com.chatsimple.netty.task.ChatHandler
import com.chatsimple.netty.task.HeartbeatHandler
import com.chatsimple.netty.task.RequestHandler
import io.netty.channel.ChannelInitializer
import io.netty.channel.socket.SocketChannel
import io.netty.handler.codec.LengthFieldBasedFrameDecoder
import java.nio.ByteOrder

class ServerChannelInitializer : ChannelInitializer<SocketChannel>() {
    override fun initChannel(sh: SocketChannel) {
        sh.pipeline()
            .addLast(TaskEncoder())
            .addLast(LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN, 4096, 0, Int.SIZE_BYTES, 0, 0, true))
            .addLast(TaskDecoder())
            .addLast(
                ServerReadHandler(
                    HeartbeatHandler(),
                    RequestHandler(),
                    ChatHandler(),
                )
            )

    }
}